<template>
    <view>
        <u-toast ref="uToast" /><u-no-network></u-no-network>
        <u-navbar title="搜索" safeAreaInsetTop fixed placeholder>
            <view class="coreshop-navbar-left-slot" slot="left">
                <u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
                <u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
                <u-icon name="home" size="22" @click="goHome"></u-icon>
            </view>
            <view slot="right">
            </view>
        </u-navbar>
        <view class="coreshop-padding-15 coreshop-margin-bottom-15 coreshop-bg-white coreshop-solid-bottom">
            <u-search placeholder="请输入关键字搜索" v-model="key" :show-action="true" action-text="搜索" :animation="false" @search="search" @custom="search"></u-search>
        </view>

            <block v-if="goodsList.length > 0">
                <view class="orderList" v-for="(goods, indexGoods) in goodsList" :key="indexGoods">
					
                    <view class="coreshop-flex coreshop-justify-between coreshop-align-center" >
                        <view class="coreshop-flex coreshop-align-center">
                            <u-icon name="paperclip" :size="20" color="rgb(94,94,94)"></u-icon>
                            <view class="coreshop-margin-left-5 coreshop-margin-right-5 coreshop-font-15 coreshop-font-weight-bold"> {{goods.name}}</view>
                        </view>
                        <view></view>
                    </view>					
					
                    <view class="item" >
						
                        <view class="left">
                            <u--image :showLoading="true" :src="goods.image" width="80px" height="80px" mode="aspectFill"></u--image>
                        </view>
<!--                        <view class="content">
                            <view class="title u-line-2">{{goods.name}}</view>
                            <view class="type">{{goods.CreateTime}}</view>
                            <view class="delivery-time"></view>
                        </view> -->
                        <view class="right">
                            <view class="decimal">销售价：￥{{ goods.price }}</view>
                            <view class="decimal">市场价：￥{{ goods.mktprice }}</view>
                            <view class="decimal" v-if="goods.isMarketable === true">商品状态：在售</view>		
                            <view class="decimal" v-if="goods.isMarketable === false">商品状态：下架</view>													
                        </view>
                    </view>
                    
                    <view class="bottom">
                       <view class="more">
                            <view class='logistics coreshop-btn' v-if="goods.isMarketable === true" @click="downGoods(goods.id)">下架</view>						   
                            <view class='logistics coreshop-btn' v-if="goods.isMarketable === false" @click="upGoods(goods.id)">上架</view>
                            <view class='logistics coreshop-btn' @click="goOrderDetail(goods.id)">编辑内容</view>
							<view class='logistics coreshop-btn' @click="goSkuProduct(goods.id)">编辑规格</view>
                        </view>

                    </view>
					
                </view>
                <u-loadmore :status="loadStatus"></u-loadmore>
            </block>
			
        <!-- 登录提示 -->
        <coreshop-login-modal></coreshop-login-modal>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                keys: [],
                key: '',
                navType: 'toNav',
                focus: true,
			    goodsList: [],	
                searchData: {
                    where: {},
                    limit: 10,
                    page: 1,
                    order: {
                        key: 'sort',
                        sort: 'asc'
                    }
                },
                searchKey: '请输入关键字搜索', //关键词				
            }
        },
        computed: {
            recommend() {
                return this.$store.state.config.recommendKeys
            }
        },
		onLoad() {
			//从第一页重新显示
			this.searchData.page = 1; 
			this.setSearchData(this.searchData, true);
			this.getGoods();
		},
        methods: {
			
            //设置查询条件
            setSearchData: function (searchData, clear = false) {
                // 深度克隆
                this.searchData = this.$u.deepClone(searchData);

                if (clear) {
                    this.goodsList = [];
                }
            },
						
            //搜索
            search: function () {
                let keys = this.key;
						
				if (keys != "")
				{
					this.searchData.page = 1; //从第一页重新显示
					this.setSearchData(this.searchData, true);
					this.getGoods();					
                    //this.$db.set('search_key', search_key);
                    //this.$db.set('search_term', keys);
                    //this.$u.route('/pages/category/list/list?key=' + keys);
                } else {
                    this.$refs.uToast.show({
                        message: '请输入要查询的关键字',
                        type: 'warning',
                    })
				}
			},
			
            //取得商品数据
            getGoods: function () {
                var _this = this;

                _this.$u.api.goodsListTow(_this.conditions()).then(res => {
                    if (res.status) {
                        if (res.data.className != '') {
                            _this.title = res.data.className;
                        } else {
                            if (res.data.where && res.data.where.searchName && res.data.where.searchName != '') {
                                _this.title = "商品搜索";
                            }
                        }
                        //_this.goodsList = _this.goodsList.concat(res.data.list);
						

                        let _list = res.data.list;
                        _this.goodsList = [..._this.goodsList, ..._list]


                        if (res.data.brands) {
                            for (let i = 0; i < res.data.brands.length; i++) {
                                res.data.brands[i].isSelect = false;
                            }
                            _this.brandList = res.data.brands;
                        }

                        if (res.data.filter) {
                            if (filter.goodsCat) {
                                for (let i = 0; i < filter.goodsCat.length; i++) {
                                    filter.goodsCat[i].isSelect = false;
                                }
                                _this.catList = filter.goodsCat;
                            }
                            if (filter.labelIds) {
                                for (let i = 0; i < filter.labelIds.length; i++) {
                                    filter.labelIds[i].isSelect = false;
                                }
                                _this.labelList = filter.labelIds;
                            }
                        }
                        if (res.data.totalPages > _this.searchData.page) {
                            _this.loadStatus = 'loadmore';
                            _this.searchData.page++;
                        } else {
                            // 数据已加载完毕
                            _this.loadStatus = 'nomore';
                        }
						
						console.log("goods:"+res);

                    }
                });
            },		
				
            // 上架
            upGoods(orderId) {
                let _this = this;
                _this.$common.modelShow('提示', '确认要商品上架吗?', () => {
                    let data = {
                        id: parseInt(orderId),
                    }
                    _this.$u.api.upGoods(data).then(res => {
                        if (res.status) {
                            _this.$refs.uToast.show({
                                message: res.msg, type: 'success', complete: function () {
                                    _this.getGoods();
									
                                }
                            })
                        } else {
                            _this.$u.toast(res.msg)
                        }
                    })
                })
            },	
					
            // 下架
            downGoods(orderId) {
                let _this = this;
                _this.$common.modelShow('提示', '确认要商品下架吗?', () => {
                    let data = {
                        id: parseInt(orderId),
                    }
                    _this.$u.api.downGoods(data).then(res => {
                        if (res.status) {
                            _this.$refs.uToast.show({
                                message: res.msg, type: 'success', complete: function () {
                                    _this.getGoods();
									
                                }
                            })
                        } else {
                            _this.$u.toast(res.msg)
                        }
                    })
                })
            },						
								
				
				
            // 统一返回筛选条件 查询条件 分页
            conditions() {
            var where = {};

                where = {
                    searchName: this.key
                };
                this.searchKey = this.key;

            this.searchData.where = where;

            this.setSearchData(this.searchData, true);
							
                let data = this.searchData;
                var newData = this.$u.deepClone(data);
                if (data.where) {
                    newData.where = JSON.stringify(data.where);
                }
                //把排序换成字符串
                if (data.order) {
                    var sort = data.order.key + ' ' + data.order.sort;
                    if (data.order.key != 'sort') {
                        sort = sort + ',sort asc'; //如果不是综合排序，增加上第二个排序优先级排序
                    }
                    newData.order = sort;
                } else {
                    newData.order = 'sort asc';
                }
                return newData;
            },				

            //清除
            deleteKey: function () {
                //删除显示
                this.keys = [];
                //删除存储
                this.$db.del('search_key');
            },

            //跳转操作
            toNav: function (keys) {
                this.$db.set('search_term', keys);
                let search_key = this.$db.get('search_key');
                if (!search_key) {
                    search_key = [];
                }
                var flag = true;
                for (var key in search_key) {
                    if (search_key[key] == keys) {
                        flag = false;
                    }
                }
                if (flag) {
                    search_key.unshift(keys);
                }
                this.$db.set('search_key', search_key);
                this.$u.route('/pages/category/list/list?key=' + keys);
            },
			
            //跳转到编辑
            goOrderDetail: function (keys) {
                this.$u.route('/pages/member/goods/goodGrounding/groundingEdit?id=' + keys);
            },
			
			//跳转到规格
			goSkuProduct: function (keys) {
                this.$u.route('/pages/member/goods/goodGrounding/groundingProductEdit?id=' + keys);
            },
			
        },
        //加载触发
        onShow(e) {
            this.keys = this.$db.get('search_key');
            this.focus = true;
        },
        //页面卸载触发
        onUnload() {
            this.$db.set('search_term', '');
        }
    }
</script>

<style lang="scss">
    page { background: #fff; }
</style>
